#!/bin/bash
set -euo pipefail

# 默认值
DEFAULT_USERNAME="jhq"
DEFAULT_TARGETARCH="arm64"
DEFAULT_VERSION="humble"
DEFAULT_BASE_IMAGE="hub.oepkgs.net/openeuler/openeuler:24.03-lts"

# 显示用法信息
show_usage() {
    echo "用法: $0 [选项]"
    echo "选项:"
    echo "  -v, --version VERSION    Docker镜像版本 (humble|openEuler|foxy|其他, 默认: $DEFAULT_VERSION)"
    echo "  -u, --user USERNAME      容器内用户名 (默认: $DEFAULT_USERNAME)"
    echo "  -a, --arch ARCH          目标架构 (arm64|amd64, 默认: $DEFAULT_TARGETARCH)"
    echo "  -b, --base BASE_IMAGE    基础镜像 (默认: $DEFAULT_BASE_IMAGE)"
    echo "  -l, --list               显示可用版本列表"
    echo "  -h, --help               显示此帮助信息"
    echo ""
    echo "示例:"
    echo "  $0 -v openEuler -u myuser -a amd64 -b hub.oepkgs.net/openeuler/openeuler:24.03"
    echo "  $0 --version humble --user developer --arch arm64 --base ubuntu:20.04"
}

# 显示可用版本列表
show_versions() {
    echo "可用版本:"
    echo "  1. humble    - 使用 Dockerfile-humble"
    echo "  2. openEuler - 使用 Dockerfile-openEuler" 
    echo "  3. foxy      - 使用 Dockerfile-foxy"
    echo "  4. 其他自定义版本"
}

# 解析命令行参数
version="$DEFAULT_VERSION"
username="$DEFAULT_USERNAME"
target_arch="$DEFAULT_TARGETARCH"
base_image="$DEFAULT_BASE_IMAGE"

while [[ $# -gt 0 ]]; do
    case $1 in
        -v|--version)
            version="$2"
            shift 2
            ;;
        -u|--user)
            username="$2"
            shift 2
            ;;
        -a|--arch)
            target_arch="$2"
            shift 2
            ;;
        -b|--base)
            base_image="$2"
            shift 2
            ;;
        -l|--list)
            show_versions
            exit 0
            ;;
        -h|--help)
            show_usage
            exit 0
            ;;
        *)
            echo "错误: 未知选项 $1" >&2
            show_usage
            exit 1
            ;;
    esac
done

# 验证架构参数
if [[ "$target_arch" != "arm64" && "$target_arch" != "amd64" ]]; then
    echo "错误: 不支持的架构 '$target_arch'，支持: arm64, amd64" >&2
    exit 1
fi

# 设置对应的 Dockerfile
case "$version" in
    humble)
        dockerfile="dockerfile/Dockerfile-humble"
        ;;
    openEuler)
        dockerfile="dockerfile/Dockerfile-openEuler"
        ;;
    foxy)
        dockerfile="dockerfile/Dockerfile-foxy"
        ;;
    *)
        # 自定义版本
        dockerfile="dockerfile/Dockerfile-${version}"
        ;;
esac

# 检查 Dockerfile 是否存在
if [ ! -f "$dockerfile" ]; then
    echo "错误: 未找到 Dockerfile: $dockerfile" >&2
    echo "可用 Dockerfile 文件:"
    find dockerfile/ -name "Dockerfile-*" -printf "  - %f\n" | sed 's/Dockerfile-//'
    exit 1
fi

# 构建标签
build_tag="agiros-docker:${version}"

# 显示构建信息
echo "=============================================="
echo "Docker 镜像构建配置"
echo "=============================================="
echo "版本:        $version"
echo "用户名:      $username"
echo "架构:        $target_arch"
echo "基础镜像:    $base_image"
echo "Dockerfile:  $dockerfile"
echo "镜像标签:    $build_tag"
echo "=============================================="

# 确认构建
read -r -p "是否开始构建? [y/N] " response
if [[ ! "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
    echo "构建已取消"
    exit 0
fi

# 执行构建
echo "开始构建 Docker 镜像..."
if docker build \
    -f "$dockerfile" \
    -t "$build_tag" \
    --build-arg USERNAME="$username" \
    --build-arg TARGETARCH="$target_arch" \
    --build-arg BASE_IMAGE="$base_image" \
    --progress=plain \
    .; then
    echo "=============================================="
    echo "✅ Docker 镜像构建成功: $build_tag"
    echo "=============================================="
    echo "使用以下命令运行容器:"
    echo "  docker run -it $build_tag"
    
    # 显示构建时使用的参数
    echo ""
    echo "构建参数:"
    echo "  USERNAME:    $username"
    echo "  TARGETARCH:  $target_arch"
    echo "  BASE_IMAGE:  $base_image"
else
    echo "=============================================="
    echo "❌ Docker 构建失败" >&2
    echo "=============================================="
    exit 2
fi


# 示例用法:
# 显示帮助
# ./docker_build.sh -h

# # 使用不同的基础镜像构建
# ./docker_build.sh -v openEuler -b "hub.oepkgs.net/openeuler/openeuler:24.03"

# # 使用 Ubuntu 基础镜像
# ./docker_build.sh -v humble -b "ubuntu:20.04" -a amd64

# # 使用不同的 openEuler 版本
# ./docker_build.sh -v openEuler -b "hub.oepkgs.net/openeuler/openeuler:23.09" -u myuser

# # 完整参数示例
# ./docker_build.sh --version foxy --base "ubuntu:18.04" --user developer --arch amd64